package com.yzt.zhuju.utils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * <pre>
 *     author : TK
 *     time   : 2017/06/20
 *     desc   : 值转换工具
 * </pre>
 */

public class FormatUtils {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd\tHH:mm:ss");
    private static final DecimalFormat dfOne = new DecimalFormat("0.0");
    private static final DecimalFormat dfTwo = new DecimalFormat("0.00");

    public static final int milliseconds_of_1minute = 60 * 1000;
    public static final int milliseconds_of_5minute = 5 * milliseconds_of_1minute;

    public static final int milliseconds_of_1hour = 60 * milliseconds_of_1minute;

    public static final int milliseconds_of_1day = 24 * milliseconds_of_1hour;

    /**
     * 转换
     *
     * @param array
     * @return
     */
    public static int[] convert(Integer[] array) {
        int[] result = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i];
        }
        return result;
    }

    /**
     * 1W以上换算成?.?w
     *
     * @param num
     * @return
     */
    public static String formatTenThousand(long num) {
        if (10_000 > num) {
            return Long.toString(num);
        }
        return formatKeepOne(num / 10_000D) + "万";
    }

    /**
     * 1K以上换算成?.?k
     *
     * @param num
     * @return
     */
    public static String formatThousand(long num) {
        if (1_000 > num) {
            return Long.toString(num);
        }
        return formatKeepOne(num / 1_000D) + "k";
    }

    /**
     * 保留一位小数
     *
     * @param d
     * @return
     */
    public static String formatKeepOne(double d) {
        return dfOne.format(d);
    }

    /**
     * 保留两位小数
     *
     * @param d
     * @return
     */
    public static String formatKeepTwo(double d) {
        return dfTwo.format(d);
    }

    /**
     * 解析成HH:MM:SS
     *
     * @param millisecond
     * @return
     */
    public static String formatTime(long millisecond) {
        if (millisecond < 1000) {
            return "00:00";
        }
        if (millisecond < 60 * 1000) {
            return "00:" + complete(millisecond / 1000);
        }
        if (millisecond < 60 * 60 * 1000) {
            return complete(millisecond / 60 / 1000) + ":" + complete((millisecond / 1000) % 60);
        }
        if (millisecond < 24 * 60 * 60 * 1000) {
            return complete(millisecond / 60 / 60 / 1000) + ":" + complete((millisecond / 60 / 1000) % 60) + ":" + complete((millisecond / 1000) % 60);
        }
        return "";
    }

    /**
     * 解析成HH:MM:SS
     *
     * @param millisecond
     * @return
     */
    public static String formatTimeFully(long millisecond) {
        if (millisecond < 1000) {
            return "00:00:00";
        }
        if (millisecond < 60 * 1000) {
            return "00:00:" + complete(millisecond / 1000);
        }
        if (millisecond < 60 * 60 * 1000) {
            return "00:" + complete(millisecond / 60 / 1000) + ":" + complete((millisecond / 1000) % 60);
        }
        if (millisecond < 24 * 60 * 60 * 1000) {
            return complete(millisecond / 60 / 60 / 1000) + ":" + complete((millisecond / 60 / 1000) % 60) + ":" + complete((millisecond / 1000) % 60);
        }
        return "";
    }

    /**
     * 解析成中文时间
     *
     * @param millisecond
     * @return
     */
    public static String formatTimeByChinese(long millisecond) {
        if (millisecond < 60 * 1000) {
            return "1分钟";
        }
        if (millisecond < 60 * 60 * 1000) {
            return millisecond / 60 / 1000 + "分钟";
        }
        if (millisecond < 24 * 60 * 60 * 1000) {
            return millisecond / 60 / 60 / 1000 + "小时" +
                    (millisecond / 60 / 1000) % 60 + "分钟";
        }
        return millisecond / 24 / 60 / 60 / 1000 + "天" +
                (millisecond / 60 / 60 / 1000) % 24 + "小时" +
                (millisecond / 60 / 1000) % 60 + "分钟";
    }

    /**
     * 解析Date
     *
     * @param millisecond
     * @return
     */
    public static String formatDate(long millisecond) {
        return sdf.format(new Date(millisecond));
    }

    private static String complete(long l) {
        if (l < 10) {
            return "0" + l;
        }
        return Long.toString(l);
    }

    /**
     * 解析成时间间隔
     *
     * @param millis
     * @return
     */
    public static String getTimeSpanByNow(final long millis) {
        long now = new Date().getTime();
        long span = now - millis;

        if (Math.abs(span) <= milliseconds_of_5minute) {
            return "刚刚";
        } else if (span < milliseconds_of_1hour) {
            return String.format(Locale.getDefault(), "%d分钟前", span / milliseconds_of_1minute);
        }
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        // 获取当天时间
        long wee = cal.getTimeInMillis();
        cal.clear();
        if (millis >= wee) {
            //%tR 24小时制
            return String.format("今天%tR", millis);
        } else if (millis >= wee - milliseconds_of_1day) {
            return String.format("昨天%tR", millis);
        } else {
            return String.format("%tF", millis);
        }
    }
}
